import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:pornhub_app/components/keep_alive_wrapper.dart';
import 'package:pornhub_app/http/api/api.dart';
import 'package:pornhub_app/http/http_service.dart';
import 'package:pornhub_app/model/blogger/blogger_fans_group.dart';
import 'package:pornhub_app/model/blogger/blogger_fans_model.dart';
import 'package:pornhub_app/model/user/user_info_model.dart';
import 'package:pornhub_app/utils/enum.dart';

import '../views/arround_community_page.dart';
import '../views/arround_compilation_page.dart';
import '../views/blogger_short_video_page.dart';
import '../views/arround_video_page.dart';
import '../views/blogger_fans_page.dart';

class BloggerDetailController extends GetxController
    with GetSingleTickerProviderStateMixin {
  final int userId;

  BloggerDetailController({required this.userId});
  List<String> tabItems = [];
  late final TabController tabController;

  BloggerFansGroupModel _bloggerFansGroup = BloggerFansGroupModel.fromJson({});
  BloggerFansGroupModel get bloggerFansGroup => _bloggerFansGroup;

  List<BloggerFansModel> _userItems = [];
  List<BloggerFansModel> get userItems => _userItems;

  RxBool isAttention = false.obs;
  Rx<UserInfo> blogger = UserInfo.fromJson({}).obs;

  List<Widget> pageItems = [];
  bool isLoadedData = false;
  RxInt currentIndex = 0.obs;
  RxString get currentIndexTitle => tabItems[currentIndex.value].obs;

  List<String> stringItems = [];
  @override
  void onInit() {
    super.onInit();
    onPageLoad();
  }

  Future<void> onPageLoad() async {
    final s = await httpInstance.post(
        url: "user/base/info",
        body: {'userId': userId},
        complete: UserInfo.fromJson);
    if (s is UserInfo == false) {
      return;
    }
    blogger.value = s;
    if (blogger.value.longVideoNum != 0) {
      tabItems.add("视频");
      pageItems.add(KeepAliveWrapper(
          child: ArroundVideoPage(
        userId: userId,
        videoLayout: layout.value,
      )));
    }
    if (blogger.value.dynNum != 0) {
      tabItems.add("G圈");
      pageItems.add(KeepAliveWrapper(
        child: ArroundCommunityPage(userId: userId),
      ));
    }
    if (blogger.value.shortVideoNum != 0) {
      tabItems.add("刷片");
      pageItems.add(KeepAliveWrapper(
        child: BloggerShortVideoPage(
          userId: userId,
          videoLayout: layout.value,
        ),
      ));
    }
    if (blogger.value.collectionNum != 0) {
      tabItems.add("合集");

      pageItems.add(KeepAliveWrapper(
        child: ArroundCompilationPage(userId: userId),
      ));
    }
    if (blogger.value.hasFansGroup != 0) {
      tabItems.add("粉丝团专属");
      pageItems.add(KeepAliveWrapper(
          child: ArroundFansPage(
        userId: userId,
        videoLayout: layout.value,
      )));
    }

    tabController = TabController(length: tabItems.length, vsync: this);
    if (blogger.value.attention != null) {
      isAttention.value = blogger.value.attention!;
    }

    if (blogger.value.age != 0) {
      stringItems.add("${blogger.value.age}");
    }
    if (blogger.value.height != null && blogger.value.height != 0) {
      stringItems.add(
        "${blogger.value.height}cm",
      );
    }

    if (blogger.value.weight != null && blogger.value.weight != 0) {
      stringItems.add(
        "${blogger.value.weight}kg",
      );
    }
    if (blogger.value.prefer != null && blogger.value.prefer!.isNotEmpty) {
      stringItems.add(
        "${blogger.value.prefer}",
      );
    }
    if (blogger.value.bodyShape != null &&
        blogger.value.bodyShape!.isNotEmpty) {
      stringItems.add(
        "${blogger.value.bodyShape}",
      );
    }
    if (blogger.value.emotion != null && blogger.value.emotion!.isNotEmpty) {
      stringItems.add(
        "${blogger.value.emotion}",
      );
    }
    if (blogger.value.intention != null &&
        blogger.value.intention!.isNotEmpty) {
      stringItems.add(
        "${blogger.value.intention}",
      );
    }
    isLoadedData = true;
    update();
  }

  Future<void> getPageDetail() async {
    var resp = await Api.queryFansGroupByUserId(userId);
    if (resp != null) {
      _bloggerFansGroup = resp;
    }
    update();
  }

  Future<bool> attentionUser(int userId) async {
    final ok =
        await Api.toogleAttentionUser(userId, attention: isAttention.value);

    isAttention.value = !isAttention.value;
    return ok;
  }

  final layout = VideoLayout.small.obs;

  void toogleLayout() => layout.value == VideoLayout.big
      ? layout.value = VideoLayout.small
      : layout.value = VideoLayout.big;
}
